package com.koron.css2.newProjectCenter.function.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.css2.ServerInterface;
import com.koron.css2.newProjectCenter.function.base.UserInfoAction;
import com.koron.css2.newProjectCenter.function.bean.UserDetail;
import com.koron.css2.newProjectCenter.function.mapper.UserInfoMapper;
import com.koron.css2.newProjectCenter.function.vo.DeptInfoVo;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.util.Constant;
import org.koron.ebs.mybatis.EnvSource;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.List;


public class UserInfoController implements ServerInterface {
    @Override
    public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
        // 将请求数据转换为Action对象
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            UserInfoAction action = objectMapper.readValue(objectMapper.writeValueAsString(req.getData()), UserInfoAction.class);
            UserInfoMapper mapper = factory.getMapper(UserInfoMapper.class, EnvSource.DEFAULT);
            // 根据账号查询用户所属部门信息
            if (action.getToDo() != null && action.getToDo() == 0) {
                DeptInfoVo vo = mapper.getDeptInfoByUserAccount(action.getAccount());
                MessageBean<DeptInfoVo> bean = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "查询成功", DeptInfoVo.class);
                bean.setData(vo);
                return bean;
            }

            // 获取部门分类
            if (action.getToDo() != null && action.getToDo() == 1) {
                List<DeptInfoVo> vos = mapper.getDeptType();
                MessageBean<Object> bean = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "查询成功", Object.class);
                bean.setData(vos);
                return bean;
            }

            // 分页获取所有用户信息
            if (action.getToDo() != null && action.getToDo() == 2) {
                PageHelper.startPage(action.getPage(), action.getPageCount());
                List<UserDetail> list = mapper.getUserInfoListByPage();
                PageInfo info = new PageInfo<>(list);
                MessageBean<PageInfo> bean = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "查询成功", PageInfo.class);
                bean.setData(info);
                return bean;
            }

        } catch (Exception e) {
            logger.error("未知错误", e);
            return MessageBean.create(Constant.ILLEGAL_PARAMETER, "未知错误", null);
        }
        return null;
    }
}
